package com.bolt.support.sqlconfig.dialect;

/**
 * Created by Administrator on 2017/8/16.
 */
public class PagingOracleDialect extends PagingDialect {

    @Override
    public String getLimitString(String sql, long offset, long limit) {
        StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
        if (offset >= 0) {
            pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
        } else {
            pagingSelect.append("select * from ( ");
        }
        pagingSelect.append(sql);
        if (offset >= 0) {
            String endString = Long.toString(offset) + "+" + Long.toString(limit);
            pagingSelect.append(" ) row_ ) where rownum_ <= ")
                    .append(endString).append(" and rownum_ > ").append(Long.toString(offset));
        } else {
            pagingSelect.append(" ) where rownum <= ").append(Long.toString(limit));
        }
        return pagingSelect.toString();
    }
}
